#include "hal_eeprom.h"
#include "cfg_param.h"
#include "common.h"
#include "hal_driver.h"
#include <hal_configer.h>
#include "sys_log.h"
#include "hal_errorcode.h"
#include "hal_EEpromWR.h"
#include "hal_tlv.h"

extern TempCurrentParaSt g_TempCurrentRealValue;

int flag = 2;

float seedEERcur = 0;
float seedEERtemp = 0;
float taEERcur = 0;
float taEERtemp = 0;
float pdEERcur = 0;

uint8_t temp = 25;

uint8_t hal_AllLaserEEPromInformInit()
{
    EEPromParaSt EEPromPara;

    memset(&EEPromPara, 0, sizeof(EEPromPara));

    if (HAL_I2cEEPromRead(EEPROM_TEMP_CURRENT_SEED_ADDR, (uint8_t *)EEPromPara.EEp_CH1_Cur, sizeof(EEPromPara)) != HAL_OK)
    {
        hal_ErrorCodeWrite(ERRORCODE_TYPE_COM_SYSTEM, s_EEProm_Rd_Abnormal_Flag, 0, ABNORMAL_STATE);
        SLOG_I("laser hal_AllLaserEEPromInformInit HAL_I2cEEPromRead error \t\r\n");
        return ABNORMAL_STATE;
    }
    else
    {
			
			//写入eeprom后要更新eeprom的flag，这里是掉电失效的
			//if这里进行读取
				HAL_I2cEEPromRead(EEPROM_TEMP_CURRENT_SEED_ADDR,(uint8_t *)EEPromPara.EEp_CH1_Cur,sizeof(EEPromPara));
				
				//flag给到标志位变量
				//flag = EEPromPara.EEp_WR_Flag;
			//flag为-1时代表eeprom未进行初始化，对eeprom内参数进行初始化
			//这里的判断应该是不等于1或2
				if(flag != 1){
				//if(flag == -1){
					//eeprom初始化值要进行更改	ddd
					//通道一电流和温度，怎么判断第一次？在EEPROM内增加标志位
					// 使用memcpy将字节数组中的数据拷贝到float类型变量中
					EEPromPara.EEp_CH1_Temp[0] = 25.00;
					EEPromPara.EEp_CH1_Cur[0] = 0.00;
					EEPromPara.EEp_CH2_Temp[0] = 25.00;
					EEPromPara.EEp_CH2_Cur[0] = 0.00;
					EEPromPara.PD_Current[0] = 0.00;
					//EEPromPara.EEp_WR_Flag = 1;
					//在这里更改flag后重新上下电看是否改变
					//写入成功，后面通过flag标志位判断是否更新过EEPROM
					//EEPromPara.EEp_WR_Flag = -1;
					//写入，逻辑有点儿问题，还未初始化完成如何进行写入?
					//已经进入else，代表没有问题
					if (HAL_I2cEEPromWrite(EEPROM_TEMP_CURRENT_SEED_ADDR, (uint8_t *)EEPromPara.EEp_CH1_Cur, sizeof(EEPromParaSt)) != HAL_OK)
					{
						hal_ErrorCodeWrite(ERRORCODE_TYPE_COM_SYSTEM, s_EEProm_Wt_Abnormal_Flag, 0, ABNORMAL_STATE);
					}
					
					bytesToFloat(&seedEERcur, &EEPromPara.EEp_CH1_Cur[0]);
					bytesToFloat(&seedEERtemp, &EEPromPara.EEp_CH1_Temp[0]);
					
					// 通道二的电流和温度
					bytesToFloat(&taEERcur, &EEPromPara.EEp_CH2_Cur[0]);
					bytesToFloat(&taEERtemp, &EEPromPara.EEp_CH2_Temp[0]);
					
					// TA功率
					bytesToFloat(&pdEERcur, &EEPromPara.PD_Current[0]);
					
				}else{
					// 通道一的电流和温度
					bytesToFloat(&seedEERcur, &EEPromPara.EEp_CH1_Cur[0]);
					bytesToFloat(&seedEERtemp, &EEPromPara.EEp_CH1_Temp[0]);
					
					// 通道二的电流和温度
					bytesToFloat(&taEERcur, &EEPromPara.EEp_CH2_Cur[0]);
					bytesToFloat(&taEERtemp, &EEPromPara.EEp_CH2_Temp[0]);
					
					// TA功率
					bytesToFloat(&pdEERcur, &EEPromPara.PD_Current[0]);
				}
					
					//SLOG_I("laser hal_AllLaserEEPromInformInit origin eeprom temp1: %.4f, current1: %.4f, temp2: %.4f, current2: %.4f, pd2: %.4f \t\r\n",
					//g_TempCurrentRealValue.Current_Chn_1, g_TempCurrentRealValue.Temperature_Chn_1,
					//g_TempCurrentRealValue.Current_Chn_2, g_TempCurrentRealValue.Temperature_Chn_2,
					//g_TempCurrentRealValue.Current_PD);
			
			
			/*
        // 通道一的电流和温度
        bytesToFloat(&g_TempCurrentRealValue.Current_Chn_1, &EEPromPara.EEp_CH1_Cur[0]);
        //bytesToFloat(&g_TempCurrentRealValue.Temperature_Chn_1, &EEPromPara.EEp_CH1_Temp[0]);
				bytesToFloat(&g_TempCurrentRealValue.Temperature_Chn_1, &temp);

        // 通道二的电流和温度
        bytesToFloat(&g_TempCurrentRealValue.Current_Chn_2, &EEPromPara.EEp_CH2_Cur[0]);
        //bytesToFloat(&g_TempCurrentRealValue.Temperature_Chn_2, &EEPromPara.EEp_CH2_Temp[0]);
				bytesToFloat(&g_TempCurrentRealValue.Temperature_Chn_2, &temp);

        // TA功率
        bytesToFloat(&g_TempCurrentRealValue.Current_PD, &EEPromPara.PD_Current[0]);

        SLOG_I("laser hal_AllLaserEEPromInformInit origin eeprom temp1: %.4f, current1: %.4f, temp2: %.4f, current2: %.4f, pd2: %.4f \t\r\n",
        g_TempCurrentRealValue.Current_Chn_1, g_TempCurrentRealValue.Temperature_Chn_1,
        g_TempCurrentRealValue.Current_Chn_2, g_TempCurrentRealValue.Temperature_Chn_2,
        g_TempCurrentRealValue.Current_PD);
			
			*/

        return NORMAL_STATE;
    }
}

void hal_AllLaserEEPromInformClear()
{
    EEPromParaSt EEPromPara;

    memset(&EEPromPara, 0xFF, sizeof(EEPromPara));

    if (HAL_I2cEEPromWrite(EEPROM_TEMP_CURRENT_SEED_ADDR, (uint8_t *)EEPromPara.EEp_CH1_Cur, sizeof(EEPromPara)) != HAL_OK)
    {
        SLOG_I("laser hal_AllLaserEEPromInformClear error\t\r\n");
    }
}
